package dao

/*
厦门大学计算机专业 | 前华为工程师
专注《零基础学编程系列》  http://lblbc.cn/blog
包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
公众号：蓝不蓝编程
*/
import (
	"time"

	uuid "github.com/satori/go.uuid"
	"gorm.io/gorm"
	"lblbc.cn/demo/dto"
	"lblbc.cn/demo/entity"
)

type OrderDao interface {
	CreateOrder(userId int64, simpleCartItemList []dto.SimpleCartItem) dto.CreateOrderResp
	QueryOrder(orderId string) dto.QueryOrderResp
	QueryByStatus(userId int64, status string) []dto.QueryOrderResp
	DeleteOrder(orderId string) string
}

type orderDbConnection struct {
	connection *gorm.DB
}

func NewOrderDao(connection *gorm.DB) OrderDao {
	return &orderDbConnection{connection: connection}
}

func (db *orderDbConnection) CreateOrder(userId int64, simpleCartItemList []dto.SimpleCartItem) dto.CreateOrderResp {
	orderId := uuid.NewV4().String()
	var userOrder entity.UserOrder
	userOrder.UserId = userId
	userOrder.ID = orderId
	userOrder.CreateTime = time.Now().Unix()
	userOrder.Status = 1
	db.connection.Save(&userOrder)

	for i := 0; i < len(simpleCartItemList); i++ {
		simpleCartItem := simpleCartItemList[i]
		var order entity.Order
		order.ID = uuid.NewV4().String()
		order.GoodsId = simpleCartItem.GoodsId
		order.OrderId = orderId
		order.Quantity = simpleCartItem.Quantity
		db.connection.Save(&order)
	}
	var result dto.CreateOrderResp
	result.OrderId = orderId
	return result
}

func (db *orderDbConnection) QueryOrder(orderId string) dto.QueryOrderResp {
	var result dto.QueryOrderResp
	var fullOrderInfos []dto.FullOrderInfo
	var userOrders []entity.UserOrder
	db.connection.Raw("SELECT * from m_user_order where id = ?", orderId).Scan(&userOrders)

	userOrder := userOrders[0]
	result.OrderId = userOrder.ID
	result.Status = userOrder.Status
	result.CreateTime = userOrder.CreateTime

	db.connection.Raw("SELECT uo.id orderId,uo.status,g.id goodsId,g.name,g.price,g.square_pic squarePic,o.quantity FROM m_user_order uo,m_orders o,m_goods g WHERE uo.id=o.order_id AND o.goods_id=g.id AND o.order_id = ?", orderId).Scan(&fullOrderInfos)
	result.List = fullOrderInfos
	return result
}

func (db *orderDbConnection) QueryByStatus(userId int64, status string) []dto.QueryOrderResp {

	var fullOrderInfos []dto.FullOrderInfo
	var userOrders []entity.UserOrder
	db.connection.Raw("select * from m_user_order where user_id = ?", userId).Scan(&userOrders)
	arrSize := len(userOrders)

	result := make([]dto.QueryOrderResp, arrSize)
	for i := 0; i < arrSize; i++ {
		userOrder := userOrders[i]
		result[i].OrderId = userOrder.ID
		result[i].Status = userOrder.Status
		result[i].CreateTime = userOrder.CreateTime

		db.connection.Raw("SELECT uo.id orderId,uo.status,g.id goodsId,g.name,g.price,g.square_pic squarePic,o.quantity FROM m_user_order uo,m_orders o,m_goods g WHERE uo.id=o.order_id AND o.goods_id=g.id AND o.order_id = ?", userOrder.ID).Scan(&fullOrderInfos)
		result[i].List = fullOrderInfos
	}

	return result
}

func (db *orderDbConnection) DeleteOrder(orderId string) string {
	db.connection.Exec("DELETE from m_orders where order_id = ?", orderId)
	db.connection.Exec("DELETE from m_user_order where id = ?", orderId)
	return ""
}
